<template>
  <div class="tinymce-editor">
    <editor v-model="myValue"
            :init="init"
            :disabled="disabled"
            ref="editor"
            @onClick="onClick">
    </editor>
    <div class="editor-custom-btn-container">
      <editorImage color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK" />
    </div>
  </div>
</template>
<script>
  import Editor from '@tinymce/tinymce-vue'
  import editorImage from './components/EditorImage'
  import plugins from './plugins'
  import toolbar from './toolbar'
  import zhCN from './zh_CN'

  export default {
    components: {
      Editor,
      editorImage,
      plugins,
      toolbar,
      zhCN
    },
    props: {
      id: {
        type: String,
        default: function () {
          return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
        }
      },
      value: {
        type: String,
        default: ''
      },
      disabled: {
        type: Boolean,
        default: false
      },
      plugins: {
        type: [String, Array],
        default: 'lists table wordcount'
      },
      toolbar: {
        type: [String, Array],
        default: 'undo redo |  formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists media table | removeformat | image |fullscreen'
      }
    },
    data () {
      return {
        init: {
          language: 'zh_CN',
          height: 300,
          plugins: plugins,
          toolbar: this.toolbar,
          quickbars: true,
          branding: false,
          menubar: true,
          cache_suffix: '?v=' + this.id,
          image_advtab: false,
          elementpath: true,
          statusbar: false
          // images_upload_url: 'travern-admin/sys/oss/upload'
          // 此处为图片上传处理函数，这个直接用了base64的图片形式上传图片，
          // 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        },
        myValue: this.value
      }
    },
    mounted () {
      window.tinymce.init({})
    },
    methods: {
      // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
      // 需要什么事件可以自己增加
      onClick (e) {
        this.$emit('onClick', e, window.tinymce)
      },
      // 可以添加一些自己的自定义事件，如清空内容
      clear () {
        this.myValue = ''
      },
      imageSuccessCBK (arr) {
        arr.forEach(v => {
          this.myValue += `<img class="wscnph" src="${v.url}">`
        })
      }

    },
    watch: {
      value (newValue) {
        this.myValue = newValue
      },
      myValue (newValue) {
        this.$emit('input', newValue)
      }
    }
  }
</script>

<style>

  .editor-upload-btn {
    display: inline-block;
  }

  /*下拉框Z轴太低无法显示问题*/
  .tox-tinymce-aux{
    z-index: 5000 !important;
  }

</style>
